package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeVo;
import jdk.nashorn.internal.ir.IfNode;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Guanxing
 * @date 2021/9/28
 */
@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存喜欢
    @Override
    public void saveUserLike(Long loginUserId, Long userLikeId) {
        //判断当前用户是否喜欢这个人
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
                        .and("likeUserId").is(userLikeId)
        );
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);

        if (userLike == null) {
            userLike = new UserLike();
            //封装对象
            userLike.setCreated(System.currentTimeMillis());
            userLike.setUserId(loginUserId);
            userLike.setLikeUserId(userLikeId);

            mongoTemplate.save(userLike);
        }

    }

    //判断双方是否相互喜欢
    @Override
    public boolean isUserLike(Long loginUserId, Long userLikeId) {
        //判断我是否喜欢对方
        Query query1 = new Query(
                Criteria.where("userId").is(loginUserId)
                        .and("likeUserId").is(userLikeId)
        );
        boolean flag1 = mongoTemplate.exists(query1, UserLike.class);

        //判断对方是否喜欢我
        Query query2 = new Query(
                Criteria.where("userId").is(userLikeId)
                        .and("likeUserId").is(loginUserId)
        );
        boolean flag2 = mongoTemplate.exists(query2, UserLike.class);

        boolean flag = flag1 && flag2;

        return flag;
    }

    //取消喜欢
    @Override
    public void deleteUserLike(Long loginUserId, Long likeUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
                        .and("likeUserId").is(likeUserId)
        );

        mongoTemplate.remove(query, UserLike.class);
    }

    //查询互相喜欢
    @Override
    public Long findEachLoveCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, Friend.class);
        return count;
    }

    //查询我的喜欢
    @Override
    public Long findLoveCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    //查询粉丝
    @Override
    public Long findFanCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("likeUserId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    //将传入的id转换为userLikeVo
    private UserLikeVo transferUserLikeVo(Long userId){
        UserLikeVo vo = new UserLikeVo();
        vo.setId(userId);
        return vo;
    }

    //查询 互相喜欢，喜欢，粉丝 访客 数据
    @Override
    public PageBeanVo findUserInfoByType(Integer pageNum, Integer pageSize, String type,Long loginUserId) {
        int startIndex = (pageNum - 1) * pageSize;
        long count = 0L;

        List<UserLikeVo> userLikeVoList = new ArrayList<>();
        Query query = null;
        //根据type查询4个表
        if (StrUtil.equals(type, "1")) {
            query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<Friend> friendList = mongoTemplate.find(query, Friend.class);
            count = mongoTemplate.count(query,Friend.class);
            //数据搬家
            if (CollectionUtil.isNotEmpty(friendList)) {
                for (Friend friend : friendList) {
                    UserLikeVo userLikeVo = transferUserLikeVo(friend.getFriendId());
                    userLikeVoList.add(userLikeVo);
                }
            }
        } else if (StrUtil.equals(type, "2")) {
            query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
            count = mongoTemplate.count(query,UserLike.class);
            //数据搬家
            if (CollectionUtil.isNotEmpty(userLikeList)) {
                for (UserLike userLike : userLikeList) {
                    UserLikeVo userLikeVo = transferUserLikeVo(userLike.getLikeUserId());
                    userLikeVoList.add(userLikeVo);
                }
            }
        } else if (StrUtil.equals(type, "3")) {
            query = new Query(
                    Criteria.where("likeUserId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
            count = mongoTemplate.count(query,UserLike.class);
            //数据搬家
            if (CollectionUtil.isNotEmpty(userLikeList)) {
                for (UserLike userLike : userLikeList) {
                    UserLikeVo userLikeVo = transferUserLikeVo(userLike.getUserId());

                    if (isUserLike(loginUserId,userLike.getUserId())) {
                        userLikeVo.setAlreadyLove(true);
                    }

                    userLikeVoList.add(userLikeVo);
                }
            }
        } else if (StrUtil.equals(type, "4")) {
            query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
            count = mongoTemplate.count(query,Visitor.class);
            //数据搬家
            if (CollectionUtil.isNotEmpty(visitorList)) {
                for (Visitor visitor : visitorList) {
                    UserLikeVo userLikeVo = transferUserLikeVo(visitor.getVisitorUserId());
                    userLikeVoList.add(userLikeVo);
                }
            }
        }


        return new PageBeanVo(pageNum,pageSize,count,userLikeVoList);
    }
}
